clear
clear mata
clear matrix

cd ..                                                                            /* Goes back to parent folder */

capture log close
log using "log/04_City_Books.log", replace


*       Patriotism!

*       FIRST VERSION  June       7, 2022
*       THIS VERSION   June       7, 2022
*       LAST RUN       June       7, 2022

*       LAST REVISOR		AT

*       Log of revisions:

*       This prepares variables from ICPRS City Books

********************************************************************************
****                      PLAN OF THE PROCEDURE                             ****
****                                                                        ****
****  1. Prepare raw data  												    ****
****  2. Merge data to correspondence with counties   					    ****
****  3. Create variables							  					    ****
****  4. Label and save  					   							    ****
********************************************************************************

do "code-build/01_Shopping_List.do"

********************************************************************************
****  1. Prepare raw data  												    ****
********************************************************************************
use "rawdata\ICPSR\07736-0001-Data.dta", clear

drop if FIPSCNTY == "000"                                 /* These are the states */

replace CC00478 = . if CC00478F == "6"                    /* CC00478 is e bond sales in 1944. CC00478F = 6 means: No Data - Geographic unit not incorporated at the time this item refers to. Codes 0 (=OK) & 5 (=a footnote exists) are fine */
replace CC00479 = . if CC00479F == "6" | CC00479F == "1"  /* CC00479 is e bond sales in 1949. CC00479F = see above. CC00479F = 1 means: No Data - Not available */
replace CC00480 = . if CC00480F == "6" | CC00480F == "1"  /* CC00480 is e bond sales in 1950. See above. Not clear what CC00480F = "*" means, but data is available there... */
replace CC00012 = . if CC00012F == "6" | CC00012F == "1" 

for VAR in var CC00443 CC00444 CC00445 CC00446 : replace VAR = . if VARF == "6"   /* VARF = 6 means: No Data - Geographic unit not incorporated at the time this item refers to. Codes 0 (=OK) & 5 (=a footnote exists) are fine */

ren CC00478 B50WARBOND44
ren CC00479 B50WARBOND49
ren CC00480 B50WARBOND50
ren CC00443 B50WARCONTRACTE
ren CC00444 B50WARCONTRACTO
ren CC00445 B50WARPROJECTI
ren CC00446 B50WARPROJECTM
ren CC00012 B50POP40

destring FIPSTATE FIPSCNTY , replace

replace   FIPSCNTY =  151 if FIPSTATE == 51 & FIPSCNTY == 154 & AREANAME == "PRINCESS ANNE"
replace   FIPSCNTY =  186 if FIPSTATE == 29 & FIPSCNTY == 193 & AREANAME == "STE GENEVIEVE"

merge 1:1 FIPSTATE FIPSCNTY                 using data/Bridge-FIPS-ICPRS , nogen keep(mat) keepus(ICPSR*)   
                                                                                                /* Matched records have the same county name (or very similar ones: eg. MCCRACKEN // MC CRACKEN or MIAMI/LYKINS	// MIAMI
                                                                                                   Not matched from using are whole states (fine too!) See above for warbonds for not matched from master.  All have missing warbonds & 0 population. */

ren ICPSR1950_STATEN stateicpsr 
ren ICPSR1950_COUNTY countyicpsr50

********************************************************************************
****  2. Merge data to correspondence with counties   					    ****
********************************************************************************

merge 1:m stateicpsr countyicpsr50 using "data/Bridge-countyicpsr1950-countynd", nogen keep(mat)                  /* Not matched are 4 counties in Hawaii from master. All values are 0 for these counties. */

gen B50WARCONTRACT = B50WARCONTRACTE + B50WARCONTRACTO
gen B50WARPROJECT  = B50WARPROJECTI  + B50WARPROJECTM
gen B50WARMONEY    = B50WARCONTRACT  + B50WARPROJECT

for VAR in var B50WARBOND44 B50WARBOND49 B50WARBOND50 B50WARCONTRACT B50WARPROJECT B50WARMONEY B50POP40  : replace VAR = VAR * weight50

foreach v of var * {
 	local l`v' : variable label `v'
    if `"`l`v''"' == "" {
		local l`v' "`v'"
	}  /* if lv = "" */
}  /* foreach v of var */

collapse (sum) B50WARBOND44 B50WARBOND49 B50WARBOND50 B50WARCONTRACT B50WARPROJECT B50WARMONEY B50POP40 , by(stateicpsr countynd)


********************************************************************************
****  3. Create variables							  					    ****
********************************************************************************

gen warbond_PC = (B50WARBOND44 / B50POP40  ) * 1000
gen warconpro_PC       = B50WARMONEY     / B50POP40


********************************************************************************
****  4. Label and save  					   							    ****
********************************************************************************

label var warbond_PC  "War bond purchases (1944) p.c. (county data book)"
label var warconpro_PC "Major war contracts + war facility projects 1940-45 p.c. (county data book)"

keeporder stateicpsr countynd warbond_PC warconpro_PC

save "data\war_bonds_contracts.dta", replace

log close
exit